{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run many Batch Normalization experiments using Cloud using ML Engine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# change these to try this notebook out\n",
    "BUCKET = 'crawles-sandbox'  # change this to your GCP bucket\n",
    "PROJECT = 'crawles-sandbox'  # change this to your GCP project\n",
    "REGION = 'us-central1'\n",
    "\n",
    "# Import os environment variables\n",
    "import os\n",
    "os.environ['BUCKET'] = BUCKET\n",
    "os.environ['PROJECT'] = PROJECT\n",
    "os.environ['REGION'] = REGION"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let’s test how Batch Normalization impacts models of varying depths. We can launch many experiments in parallel using Google Cloud ML Engine. We will fire off 14 jobs with varying hyperparameters:\n",
    "\n",
    "* With and without Batch Normalization\n",
    "* Varying model depths from 1 hidden layer to 7 hidden layers\n",
    "\n",
    "We use the [tf.estimator](https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator) API to build a model and deploy it using [Cloud ML Engine](https://cloud.google.com/ml-engine/docs/technical-overview)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PKG-INFO  setup.cfg \u001b[1m\u001b[31msetup.py\u001b[m\u001b[m  \u001b[1m\u001b[36mtrainer\u001b[m\u001b[m\n",
      "__init__.py \u001b[1m\u001b[36m__pycache__\u001b[m\u001b[m model.py    task.py\n"
     ]
    }
   ],
   "source": [
    "!ls mnist_classifier/\n",
    "!ls mnist_classifier/trainer/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "submitMLEngineJob() {\n",
    "    gcloud ml-engine jobs submit training $JOBNAME \\\n",
    "        --package-path=$(pwd)/mnist_classifier/trainer \\\n",
    "        --module-name trainer.task \\\n",
    "        --region $REGION \\\n",
    "        --staging-bucket=gs://$BUCKET \\\n",
    "        --scale-tier=BASIC \\\n",
    "        --runtime-version=1.4 \\\n",
    "        -- \\\n",
    "        --outdir $OUTDIR \\\n",
    "        --hidden_units $net \\\n",
    "        --num_steps 10 \\\n",
    "        $batchNorm\n",
    "}\n",
    "\n",
    "# submit for different layer sizes\n",
    "export PYTHONPATH=${PYTHONPATH}:${PWD}/mnist_classifier\n",
    "for batchNorm in '' '--use_batch_normalization'\n",
    "do\n",
    "    net=''\n",
    "    for layer in 500 400 300 200 100 50 25;\n",
    "    do\n",
    "        net=$net$layer\n",
    "        netname=${net//,/_}${batchNorm/--use_batch_normalization/_bn}\n",
    "        echo $netname\n",
    "        JOBNAME=mnist$netname_$(date -u +%y%m%d_%H%M%S)\n",
    "        OUTDIR=gs://${BUCKET}/mnist_models/mnist_model$netname/trained_model\n",
    "        echo $OUTDIR $REGION $JOBNAME\n",
    "        gcloud storage rm --recursive --continue-on-error $OUTDIR\n",
    "        submitMLEngineJob\n",
    "        net=$net,    \n",
    "    done \n",
    "done"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright 2018 Google Inc. All Rights Reserved.\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  },
  "nav_menu": {},
  "toc": {
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 6,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
